home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: giuliano@ix.netcom.com(Giuliano Carlini)
- Newsgroups: comp.lang.c++
- Subject: Re: String operator+ and memory leakage.
- Date: 19 Apr 1996 18:38:37 GMT
- Organization: Netcom
- Message-ID: <4l8mjd$flq@reader2.ix.netcom.com>
- References: <4l5fok$feo@utopia.hacktic.nl>
- NNTP-Posting-Host: lbx-ca7-26.ix.netcom.com
- X-NETCOM-Date: Fri Apr 19 11:38:37 AM PDT 1996
-
- In <4l5fok$feo@utopia.hacktic.nl> Mike Tavares <MIKET@cdynamics.com>
- writes:
- >
- >I have an implementation (not mine) of the String class that
- >is leaking memory during the + operator code. The code follows:
- > <major snip>
- >String&
- >String::operator+( String& a_string )
- > {
- > int total_size = string_length + strlen( a_string ) + 1;
- > char* temp_array = new char[total_size];
- > char* char_array = a_string;
- >
- > strcpy( temp_array, character_array );
- > strcat( temp_array, char_array );
- >
- > String* new_string = new String( temp_array );
- > delete [] temp_array;
- > return *new_string;
- > }
- > <more snipping>
- >
- >My usage is:
- >
- >destString = string1 + string2 + string3...;
- >
- >
- >The problem is, when new_string is created and returned through a
- >reference, no one ever deletes it.
-
- DISCLOSURE: I license a 16 bit garbage collector to geodesic systems,
- and hope to earn royalies from its sale.
-
- The solution - for me - is to use garbage collecter. The design is nice
- and general purpose. Changing it to avoid this problem will make
- String's less useful.
-
- This is a perfect example of the real benefit of garbage collector's.
- They permit better designs, and of clearer code. Without them, you
- either have to mess up the class interface, or the client code.
-
- You can find out more about garbage collector's for C++ at either:
- http://www.geodesic.com - a commercial GC
- ftp://parcftp.xerox.com/pub/gc - a freely available GC
-
-
- g
-